package com.sfx.admin.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sfx.admin.common.convention.result.Result;
import com.sfx.admin.common.convention.result.Results;
import com.sfx.admin.remote.dto.req.*;
import com.sfx.admin.remote.dto.resp.PageRecycleBinRespDTO;
import com.sfx.admin.remote.dto.resp.ShortLinkStatsAccessRecordRespDTO;
import com.sfx.admin.remote.dto.resp.ShortLinkStatsRespDTO;

import java.util.HashMap;
import java.util.Map;

public interface ShortLinkRemoteService {

    default void saveRecycleBin(SaveRecycleBinReqDTO requestParam) {
        HttpUtil.post("localhost:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
        Results.success();
    }

    default Result<IPage<PageRecycleBinRespDTO>> pageRecycleBin(PageRecycleBinReqDTO requestParam) {
        Map<String, Object> map = new HashMap<>();
        map.put("current", requestParam.getCurrent());
        map.put("size", requestParam.getSize());
        String get = HttpUtil.get("localhost:8001/api/short-link/v1/recycle-bin/page", map);
        return JSON.parseObject(get, new TypeReference<>() {
        });
    }

    default void recoverRecycleBin(RecoverRecycleBinReqDTO requestParam) {
        HttpUtil.post("localhost:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
        Results.success();
    }


    default void removeRecycleBin(RemoveRecycleBinReqDTO requestParam) {
        HttpUtil.post("localhost:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
        Results.success();
    }

    /**
     * 访问单个短链接指定时间内监控数据  full-short_url + gid(单个短链接)
     */
    default Result<ShortLinkStatsRespDTO> shortLinkStats(ShortLinkStatsReqDTO requestParam) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("fullShortUrl", requestParam.getFullShortUrl());
        paramMap.put("gid", requestParam.getGid());
        paramMap.put("startDate", requestParam.getStartDate());
        paramMap.put("endDate", requestParam.getEndDate());
        String get = HttpUtil.get("localhost:8001/api/short-link/v1/stats", paramMap);
        return JSON.parseObject(get, new TypeReference<Result<ShortLinkStatsRespDTO>>() {
        });
    }

    /**
     * 访问单个短链接指定时间内访问记录监控数据
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> shortLinkStatsAccessRecord(ShortLinkStatsAccessRecordReqDTO requestParam) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("fullShortUrl", requestParam.getFullShortUrl());
        paramMap.put("gid", requestParam.getGid());
        paramMap.put("startDate", requestParam.getStartDate());
        paramMap.put("endDate", requestParam.getEndDate());
        String get = HttpUtil.get("localhost:8001/api/short-link/v1/stats/access-record", paramMap);
        return JSON.parseObject(get, new TypeReference<>() {
        });
    }

    /**
     * 访问分组短链接指定时间内监控数据
     */
    default Result<ShortLinkStatsRespDTO> groupShortLinkStats(ShortLinkGroupStatsReqDTO requestParam) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("gid", requestParam.getGid());
        paramMap.put("startDate", requestParam.getStartDate());
        paramMap.put("endDate", requestParam.getEndDate());
        String get = HttpUtil.get("localhost:8001/api/short-link/v1/stats/group", paramMap);
        return JSON.parseObject(get, new TypeReference<>() {
        });
    }

    /**
     * 访问分组短链接指定时间内访问记录监控数据
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> groupShortLinkStatsAccessRecord(ShortLinkGroupStatsAccessRecordReqDTO requestParam) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("gid", requestParam.getGid());
        paramMap.put("startDate", requestParam.getStartDate());
        paramMap.put("endDate", requestParam.getEndDate());
        String get = HttpUtil.get("localhost:8001/api/short-link/v1/stats/access-record/group", paramMap);
        return JSON.parseObject(get, new TypeReference<>() {
        });
    }

    default Result<String> getTitleByUrl(String url) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("url", url);
        String get = HttpUtil.get("localhost:8001/api/short-link/v1/title", paramMap);
        return JSON.parseObject(get, new TypeReference<>() {
        });
    }
}